/**
 * axios 请求
 */
import axios from "axios";
import { message } from "ant-design-vue";

import history from "../router";

import config from "~/config";

const MODE = import.meta.env.MODE; // 环境变量

import router from "../router";
import { userStore } from "../stores/modules/user";

function requestImg(file) {
  const store = userStore();
  //处理路径问题
  let base = config[MODE];
  let param = new FormData(); // 创建form对象
  param.append("image", file); // 通过append向form对象添加数据
  param.append("platform", "pc");
  param.append("version", "1.0");
  param.append("is_editor", 1);
  let headerParams = {
    data: param,
    headers: {
      Authorization: "Bearer " + store.userInfo.access_token || "",
    },
  };
  let method = "POST";
  let url = "/home/ossUpload";
  //赋值完全部路径
  return axios({
    baseURL: base.apiBaseUrl, // 请求域名地址
    url,
    method,
    ...headerParams,
  }).then((res) => {
    if (res.data.code === -2) {
      //清除token
      store.$reset();
      router.push({ path: "/login" });
      return;
    } else if (res.data.code === -3) {
      message.error(res.data.msg);
      //没有权限，先不处理
      // history.push('/exception/403');
      return;
    } else if (res.data.code === -1) {
      message.error(res.data.msg);
      // return;
    } else if (res.data.code === -4) {
      message.error(res.data.msg);
      history.push("/");
      // return;
    }
    return res.data;
  });
}

export default requestImg;
